class ClientManager {

    private constructor() {
    }


    private static _ins: ClientManager
    static ins() {
        if (!this._ins) {
            this._ins = new ClientManager()
        }
        return this._ins;
    }

    public async login() {
        try {
            await Pomelo.ins().init({
                host: PomeloConsts.Host,
                port: PomeloConsts.Port
            });
            let res: any = await Pomelo.ins().request(RequestRoute.GateHandlerQueryEntry, null);
            Pomelo.ins().disconnect();
            res = await Pomelo.ins().init({
                host: res.host,
                port: res.port
            });
            let userInfo: any = await Pomelo.ins().request(RequestRoute.ConnectorHandlerEntry, RequestRoute.GateHandlerQueryEntryMsg(LoginType.Test));
            GameGlobal.ins().user = userInfo.data;
            console.log("登录成功..");
            return true;
        }
        catch (e) {
            console.error("登录失败", e)
            return false;
        }

    }

    public async buyTreasure(quality) {

        try {
            let data: any = await Pomelo.ins().request(RequestRoute.FightBuyTreasure, { qualityType: quality });
            EffectUtils.showTips("购买成功!", 1, GameGlobal.ins().TextColors.green);
            GameGlobal.ins().user.player.money -= Number(GameGlobal.ins().treasureConfig[quality].price);
            return data.data;
        }
        catch (e) {
            console.error("购买失败", e)
            if (e.msg) {
                EffectUtils.showTips(e.msg, 1, GameGlobal.ins().TextColors.red);
            }
            else {
                EffectUtils.showTips("购买失败,服务器出错", 1, GameGlobal.ins().TextColors.red);
            }
            return false;
        }
    }

    public async openTreasue(obj, item) {
 
        let aninTimeFlag = Date.now();
        console.log(aninTimeFlag);
        EffectUtils.loopScaleXY(obj);
        try {
            let data: any = await Pomelo.ins().request(RequestRoute.FightOpenTreasure, { id: item.id });
            let tipText = null;
            let tipColor = 0;
            let timeMs = 1500;
            if (data.data.shield) {
                let shield = data.data.shield;
                tipText = "获得 " + shield.qualityType + "色 盾牌 lv" + shield.level + " HP+" + shield.realHp;
                tipColor = GameGlobal.ins().treasureConfig[shield.qualityType].color;
                timeMs = 5000
                GameGlobal.ins().user.player.shield = data.data.shield;
            }
            else if (data.data.weapon) {
                let weapon = data.data.weapon;
                tipText = "获得 " + weapon.qualityType + "色 武器 lv" + weapon.level + " 攻击+" + weapon.realAttack;
                tipColor = GameGlobal.ins().treasureConfig[weapon.qualityType].color;
                timeMs = 5000
                GameGlobal.ins().user.player.weapon = data.data.weapon;
            }
            else if (data.data.rewardMoney) {
                tipText = "获得金币" + data.data.rewardMoney + "个"
                tipColor = GameGlobal.ins().TextColors.golden;

                GameGlobal.ins().user.player.money += data.data.rewardMoney;
            }
            else {
                tipText = data.data;
                tipColor = GameGlobal.ins().TextColors.gray;
            }
            let t = Date.now() - aninTimeFlag;
            if (t < 500) {
                await TimerUtils.delay(500 - t);
            }
       
            EffectUtils.showTips(tipText, 1, tipColor, undefined, timeMs);
            EffectUtils.bomb01(obj, "bomb01_particle" + item.qualityType + "_png");
            SceneManager.instance.mainScene.dataGroupTreasureCollection.removeItemAt(SceneManager.instance.mainScene.dataGroupTreasureCollection.getItemIndex(item));
        }
        catch (e) {
            console.error(e)
            return false;
        }
        egret.Tween.removeTweens(obj);
    }

    public async getTreasure() {
        try {
            let data: any = await Pomelo.ins().request(RequestRoute.FightGetTreasure);
            GameGlobal.ins().treasure = data.data;
            return true;
        }
        catch (e) {
            console.error(e)
            return false;
        }

    }

    public async getConfig() {
        try {
            let config: any = await Pomelo.ins().request(RequestRoute.FightGetTreasureConfig);
            GameGlobal.ins().treasureConfig = config.data;
            console.log("获取配置成功..");
            return true;
        }
        catch (e) {
            console.error("获取失败", e)
            return false;
        }
    }



}